﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Sunyi.Project.WebPoint.DataContract;

namespace Sunyi.Project.WebPoint.DataAccessController
{
    public partial class UserDaoController
    {
        public User SelectByName(string userName)
        {
            User user = null;

            Dictionary<Guid, object> users = CacheProvider.Instance.Get<User>();

            foreach (object obj in users.Values)
            {
                User item = obj as User;

                if (item != null && item.UserName == userName)
                {
                    user = item;
                    break;
                }
            }

            if (user == null)
            {
                user = dao.SelectByName(userName);
            }

            if (user != null)
            {
                dao.FillRoles(user);
                CacheProvider.Instance.Add<User>(user.Id, user);
            }

            return user;
        }

        public int SearchCount(string userName, string roleName, bool isActive)
        {
            return dao.SearchCount(userName, roleName, isActive);
        }

        public Collection<User> Search(string userName, string roleName, bool isActive, int pageIndex, int pageSize)
        {
            return dao.Search(userName, roleName, isActive, pageIndex, pageSize);
        }
    }
}
